// pages/login/login.js
const app = getApp();
const util = require("../../utils/util.js");
const api = require('../../utils/api.js');
const maxTime = 60;
var currentTime = maxTime;
var interval;
Page({
    data: {
        mainColor: app.pageColor.mainColor,
        phoneVal: '',
        codeVal: '',
        userCodeVal:'',
        text: "获取验证码",
        btndisabled: false,
    },
    onLoad: function (options) {
        
    },
    onReady: function () {
        util.setNavigationBarTitle("登录");
    },
    // 获取手机号
    getPhone: function (e) {
        this.setData({
            phoneVal: e.detail.value
        })
    },
    // 获取用户输入的验证码
    getCodeVal: function (e) {
        this.setData({
            userCodeVal: e.detail.value
        })
        console.log(this.data.userCodeVal);
    },
    // 点击按钮获取验证码
    getCode: function () {
        var that = this;
        let url = api.sendNote();
        if (!util.IsMobile(that.data.phoneVal)) {
            util.failToast("请输入正确手机！");
            return;
        };
        util.http(true,"POST", url, {phone: that.data.phoneVal}, res => {
            if(res.code==1){
                wx.showModal({
                    showCancel: false,
                    content: res.message,
                })
                return;
            }else{
                that.reSendPhoneNum();
                that.setData({
                    codeVal: res.code
                })
                console.log(that.data.codeVal);
            }
        })
    },
    // 60S验证
    reSendPhoneNum: function () {
        if (currentTime > 0) {
            var that = this
            currentTime = maxTime
            interval = setInterval(function () {
                that.setData({
                    text: currentTime + '(s)后重新发送',
                    btndisabled: true
                })
                currentTime--
                if (currentTime <= 0) {
                    currentTime = 60
                    clearInterval(interval);
                    that.setData({
                        text: "重新发送",
                        btndisabled: false
                    })
                }
            }, 1000)
        } else {
            wx.showToast({
                title: '短信已发送！',
                icon: 'loading',
                duration: 700
            })
        }
    },
    // 登录
    logIn: function () {
        let that = this;
        let url = api.login();
        let [phone, code, userCodeVal] = [that.data.phoneVal, that.data.codeVal, that.data.userCodeVal];
        if (!util.IsMobile(phone)) {
            util.failToast("请输入正确手机！");
            return;
        };
        if (userCodeVal.length!=6) {
            util.failToast("验证码错误！");
            return;
        }
        util.http(true, 'POST', url,{
            phone: phone,
            code: userCodeVal
        },res=>{
            util.setStorage("token", res.token);
            wx.navigateBack();
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        currentTime = maxTime
        if (interval != null) {
            clearInterval(interval)
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})